#include"List.h"



ListNode* ListCreate()
{
	ListNode* Node = (ListNode*)malloc(sizeof(ListNode));
	Node->next = Node;
	Node->prev = Node;
	return Node;
}
void ListPushBack(ListNode* pHead, LTDataType x)
{
	assert(pHead);

	//ListNode* NowNode = ListCreate();
	//ListNode* headprev = pHead->prev;
	//pHead->prev = NowNode;
	//NowNode->next = pHead;
	//headprev->next = NowNode;
	//NowNode->prev = headprev;

	//NowNode->data = x;
	ListInsert(pHead, x);
}

void ListPrint(ListNode* pHead)
{
	//assert(pHead->next != pHead);

	ListNode* cur = pHead->next;
	printf("head<->");
	while (cur != pHead)
	{
		printf("%d<->", cur->data);
		cur = cur->next;
	}
	printf("\n");
}

void ListDestory(ListNode* pHead)
{
	assert(pHead);

	ListNode* cur = pHead->next;
	ListNode* curNext;
	while (cur != pHead)
	{
		curNext = cur->next;
		free(cur);
		cur = curNext;
	}
	free(pHead);

}

void ListPopBack(ListNode* pHead)
{
	assert(pHead);
	assert(pHead->prev);

	/*ListNode* pHeadpre = pHead->prev;
	pHead->prev->prev->next = pHead;
	pHead->prev = pHead->prev->prev;
	free(pHeadpre);*/
	ListErase(pHead->prev);
}

void ListPushFront(ListNode* pHead, LTDataType x)
{
	assert(pHead);

	/*ListNode* NowNode = ListCreate();
	NowNode->next = pHead->next;
	pHead->next->prev = NowNode;
	pHead->next = NowNode;
	NowNode->prev = pHead;
	NowNode->data = x;*/
	ListInsert(pHead->next, x);
}
void ListPopFront(ListNode* pHead)
{
	assert(pHead);
	assert(pHead->next != pHead);

	/*ListNode* pHradNext = pHead->next;
	pHead->next->next->prev = pHead;
	pHead->next = pHead->next->next;
	free(pHradNext);*/
	ListErase(pHead->next);
}

ListNode* ListFind(ListNode* pHead, LTDataType x)
{
	assert(pHead);
	assert(pHead->next != pHead);

	ListNode* cur = pHead->next;
	while (cur!=pHead)
	{
		if (cur->data == x)
			return cur;
		cur = cur->next;
	}
	return NULL;
}

void ListInsert(ListNode* pos, LTDataType x)
{
	assert(pos);

	ListNode* NowNode = ListCreate();
	NowNode->prev = pos->prev;
	NowNode->next = pos;
	pos->prev->next = NowNode;
	pos->prev = NowNode;
	NowNode->data=x;
}


void ListErase(ListNode* pos)
{
	assert(pos);

	pos->prev->next = pos->next;
	pos->next->prev = pos->prev;
	free(pos);

}


void stackPush(struct stack* pSL, char x)
{
	assert(pSL);
	if (pSL->top == pSL->cavlity)
	{
		char* p = (char*)realloc(pSL->a, sizeof(char) * 2 * pSL->cavlity);
		if (p == NULL)
			exit(-1);
		pSL->a = p;
	}
	pSL->a[pSL->top] = x;
	pSL->top++;

}